<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**

 * @package     ceo
 * @subpackage  models
 * @category    models
 * @author      ydd team, wu
 * @copyright   copyright (c) 2012, yaoduoduo, Inc.
 * @filesource
 */
class Demand_model extends Base_model {

    protected $_table_name = 'demand';
    protected $_key_name = 'demand_id';

    function get_demand_info($id) {
        $this->db->where($this->_key_name, $id);
        $query = $this->db->get($this->_table_name);

        return $query->row();
         /*
          if(!empty($id))
          $where[$this->_key_name]=$id;
          $user_purview=check_broker_is_visible();
          if(!$user_purview){
          $this->set_or_like('front_man,broker',$this->login_lib->users_name());
          }
          $where['dtime']=0;
          return $this->fetch(NULL, $where,NULL,'row');
         */
    }



    function get_has_client_id() {
        $this->db->where('client_id > ' ,0);
        $q=$this->db->get($this->_table_name);
        return $q;

    }
    function update_demand_point_count($id) {

        $sql = "UPDATE  `h_demand` SET  `point_count` =  `point_count` +1 WHERE  `demand_id` = $id";
        $this->db->query($sql);
    }

    function get_one_demand_info($id) {
        $this->db->where($this->_key_name, $id);
        $res = $this->db->get($this->_table_name);
        return $res;
    }

    function get_demand_do_excel($post_ids) {

        if ($post_ids) {

            $this->db->select(  'h_demand.* , '.
                            'h_client.name as  c_name , '.
                            'h_client.mobile as  c_mobile   ,'.
                            'h_client.home_phone as  c_home_phone  ,'.                            

                            'h_client.company_phone as  c_company_phone , '.
                            'h_client.home_address as  c_home_address , '.
                            'h_client.company_address as  c_company_address , '.
                            'h_client.home_building_name as  c_home_building_name , '.
                            //$val->c_home_building_name
                            'h_client.company_name as  c_company_name , '.
                            'h_client.email as  c_email , '.


                            'h_client_contact.name as  c_c_name , '.
                            'h_client_contact.home_phone as  c_c_mobile , '
                            
                            );
            foreach ($post_ids as $num => $id) {
                $this->db->or_where('h_demand.demand_id', $id);
                //echo $id.'/';
            }
        $this->db->from($this->_table_name);

        $this->db->join('h_client','h_client.client_id = h_demand.client_id');
        $this->db->join('h_client_contact','h_client_contact.client_contact_id = h_demand.client_contact_id');

            

        }

        $res = $this->db->get( );
        return $res;
    }

	
	
	function update($id,$data) {		
		$this->db->where('demand_id',$id);
		$this->db->update("h_demand",$data);
		
	}
	
    function get_field() {

        return $this->db->list_fields($this->_table_name);        
    }


    //個人erson
    function get_demand_person_excel() {
        
        //是否經紀人        
        $CI = &get_instance();
        $purview = $CI->login_lib->purview();
        $users_id = $CI->login_lib->users_id();

        if ($purview!='#all') {

            //$this->db->where('front_man', $users_id);
            $this->db->where('broker', $users_id);
        }

        $this->db->select(  'h_demand.* , '.
                            'h_client.name as  c_name , '.
                            'h_client.mobile as  c_mobile   ,'.
                            'h_client.home_phone as  c_home_phone  ,'.                            

                            'h_client.company_phone as  c_company_phone , '.
                            'h_client.home_address as  c_home_address , '.
                            'h_client.company_address as  c_company_address , '.
                            'h_client.home_building_name as  c_home_building_name , '.
                            //$val->c_home_building_name
                            'h_client.company_name as  c_company_name , '.
                            'h_client.email as  c_email , '.


                            'h_client_contact.name as  c_c_name , '.
                            'h_client_contact.home_phone as  c_c_mobile , '
                            
                            );
        $this->db->from($this->_table_name);

        $this->db->join('h_client','h_client.client_id = h_demand.client_id');
        $this->db->join('h_client_contact','h_client_contact.client_contact_id = h_demand.client_contact_id');


        $res    =   $this->db->get();

        return $res;
    }

    //公司excel
    function get_demand_company_excel($fiels) {  

        $ff_flag=true;
        $_sel_str='';
        foreach($fiels as $_k =>$f_name){

            if($ff_flag){
                $ff_flag=false;
                $_sel_str.=$f_name;
            }else{
                $_sel_str.=",".$f_name;
            }

        }
        
        $this->db->select($_sel_str);
        

        $res = $this->db->get($this->_table_name);
        return $res;
    }

    function get_demand_name($name) {
        if (!empty($name))
            $where["name"] = $name;
        $user_purview = check_broker_is_visible();
        if (!$user_purview) {
            $this->set_or_like('front_man,broker', $this->login_lib->users_name());
        }
        $where['dtime'] = 0;
        return $this->fetch(NULL, $where, NULL, 'row');
    }

    function get_demand_name_count($name) {
        if ($name != "")
            $where["name"] = $name;
        $user_purview = check_broker_is_visible();
        if (!$user_purview) {
            $this->set_or_like('front_man,broker', $this->login_lib->users_name());
        }
        $where['dtime'] = 0;
        return $this->fetch(NULL, $where, NULL, 'count');
    }

    function get_demand($uri_query, $limit) {
        $this->set_limit($limit);
        $user_purview = check_broker_is_visible();
        if (!$user_purview) {
            $this->set_or_like('front_man,broker', $this->login_lib->users_name());
        }
        $where = array('dtime' => 0, 'name' => $uri_query['keyword']);
        return $this->fetch(NULL, $where);
    }

    function get_demand_count($uri_query) {
        $user_purview = check_broker_is_visible();
        if (!$user_purview) {
            $this->set_or_like('front_man,broker', $this->login_lib->users_name());
        }
        $con["where"][] = array('dtime' => 0, 'name' => $uri_query['keyword']);
        $this->set_args_array($con);
        return $this->fetch_count();
    }

    function get_all() {
        $user_purview = check_broker_is_visible();
        if (!$user_purview) {
            $this->set_or_like('front_man,broker', $this->login_lib->users_name());
        }
        $where = array('dtime' => 0);
        return $this->fetch(NULL, $where);
    }

    function demand_pair($where = null, $w = null, $limit) {

        $this->set_limit($limit);
        $user_purview = check_broker_is_visible();
        if (!$user_purview) {
            $this->set_or_like('front_man,broker', $this->login_lib->users_name());
        }
        $where['dtime'] = 0;
        return $this->fetch(NULL, $where);
    }

    function demand_pair_count($where = null, $w = null) {
        $user_purview = check_broker_is_visible();
        if (!$user_purview) {
            $this->set_or_like('front_man,broker', $this->login_lib->users_name());
        }
        $where['dtime'] = 0;
        return $this->fetch(NULL, $where, NULL, "count");
    }

    //配對
    function match_demand($where) {

        $this->db->where($where);
        $this->db->order_by("ctime", "DESC");

        $query = $this->db->get("h_demand");
        return $query;
    }

    //查詢
    function query_demand($w = null, $keyword = null, $limit) {
        $this->db->order_by("ctime", "DESC");
        $this->set_limit($limit);
        if ($keyword != "")
            $this->set_or_like('name', $keyword);
        if (isset($w['name']))
            $this->set_or_like('name', $w['name']);
        if (isset($w['required_level']))
            $where['required_level'] = $w['required_level'];
        if (isset($w['type_rent']))
            $this->db->or_like('type_rent', $w['type_rent']);
        if (isset($w['type_sell']))
            $this->db->or_like('type_sell', $w['type_sell']);

        //if(isset($w['type_consignment'])){
        //$this->db->where('demand_id', '32');
        //}

        if (isset($w['object_usage']))
            $where['object_usage'] = $w['object_usage'];
        if (isset($w['building_area1']))
            $where['building_area1 >='] = $w['building_area1'];
        if (isset($w['building_area2']))
            $where['building_area2 <='] = $w['building_area2'];
        if (isset($w['indoor_area1']))
            $where['indoor_area1 >='] = $w['indoor_area1'];
        if (isset($w['indoor_area2']))
            $where['indoor_area2 <='] = $w['indoor_area2'];
        if (isset($w['land_area1']))
            $where['land_area1 >='] = $w['land_area1'];
        if (isset($w['land_area2']))
            $where['land_area2 <='] = $w['land_area2'];
        if (isset($w['total_budget1']))
            $where['total_budget1 >='] = $w['total_budget1'];
        if (isset($w['total_budget2']))
            $where['total_budget2 <='] = $w['total_budget2'];
        //fix by vicotr
        if (isset($w['key_areas1']))
            $where['key_areas1'] = $w['key_areas1'];
        if (isset($w['key_areas2']))
            $where['key_areas2'] = $w['key_areas2'];
        //fix by victor

        if (isset($w['key_areas']))
            $this->set_or_like('key_areas1,key_areas2', $w['key_areas']);

        if (isset($w['specify_building1']))
            $this->set_or_like('specify_building1', $w['specify_building1']);
        if (isset($w['specify_building2']))
            $this->set_or_like('specify_building2', $w['specify_building2']);

        if (isset($w['object_name']))
            $this->set_or_like('object_name1,object_name2', $w['object_name']);
        if (isset($w['sections1']))
            $this->set_or_like('sections1,sections2', $w['sections1']);
        if (isset($w['sections2']))
            $this->set_or_like('sections1,sections2', $w['sections2']);
        if (isset($w['administrative_area']))
            $this->set_or_like('administrative_area1,administrative_area2', $w['administrative_area']);
        if (isset($w['year_of_construction1']))
            $where['year_of_construction >='] = $w['year_of_construction1'];
        if (isset($w['year_of_construction2']))
            $where['year_of_construction <='] = $w['year_of_construction2'];
        if (isset($w['parking_number']))
            $where['parking_number >='] = $w['parking_number'];
        if (isset($w['status']))
            $where['status'] = $w['status'];
        if (isset($w['customer_source']))
            $where['customer_source'] = $w['customer_source'];

        if (isset($w['sctime'])) {
            $ts = explode("/", $w['sctime']);
            $where['ctime >='] = mktime(0, 0, 0, $ts[1], $ts[2], $ts[0]);
        }
        if (isset($w['ectime'])) {
            $te = explode("/", $w['ectime']);
            $where['ctime <='] = mktime(23, 59, 59, $te[1], $te[2], $te[0]);
        }
        if (isset($w['broker']))
            $this->set_or_like('broker', $w['broker']);
        $user_purview = check_broker_is_visible();
        if (!$user_purview) {
            $this->set_or_like('front_man,broker', $this->login_lib->users_name());
        }

        if (!empty($w['type_consignment'])) {

            //	echo "thanks~~~dah omm";

            $where['type_consignment >='] = $w['type_consignment'];
        }


        if (empty($where)) {


            $qe = $this->db->get($this->_table_name);

            return $qe->result();
        } else {

            return $this->fetch($fields = NULL, $where, $limit = NULL, $back = "result");
        }
        //return $this->fetch(NULL, $where);
    }

    function query_demand_count($w = null, $keyword = nulls) {
        if ($keyword != "")
            $this->set_or_like('name', $keyword);
        if (isset($w['name']))
            $this->set_or_like('name', $w['name']);
        if (isset($w['required_level']))
            $where['required_level'] = $w['required_level'];
        if (isset($w['type']))
            $where['type'] = $w['type'];
        if (isset($w['object_usage']))
            $where['object_usage'] = $w['object_usage'];
        if (isset($w['building_area1']))
            $where['building_area1 >='] = $w['building_area1'];
        if (isset($w['building_area2']))
            $where['building_area2 <='] = $w['building_area2'];
        if (isset($w['indoor_area1']))
            $where['indoor_area1 >='] = $w['indoor_area1'];
        if (isset($w['indoor_area2']))
            $where['indoor_area2 <='] = $w['indoor_area2'];
        if (isset($w['land_area1']))
            $where['land_area1 >='] = $w['land_area1'];
        if (isset($w['land_area2']))
            $where['land_area2 <='] = $w['land_area2'];
        if (isset($w['total_budget1']))
            $where['total_budget1 >='] = $w['total_budget1'];
        if (isset($w['total_budget2']))
            $where['total_budget2 <='] = $w['total_budget2'];
        if (isset($w['key_areas']))
            $this->set_or_like('key_areas1,key_areas2', $w['key_areas']);

        if (isset($w['specify_building']))
            $this->set_or_like('specify_building1,specify_building2', $w['specify_building']);
        if (isset($w['object_name1']))
            $this->set_or_like('object_name11,object_name12', $w['object_name1']);
        if (isset($w['sections']))
            $this->set_or_like('sections1,sections2', $w['sections']);
        if (isset($w['administrative_area']))
            $this->set_or_like('administrative_area1,administrative_area2', $w['administrative_area']);
        if (isset($w['year_of_construction1']))
            $where['year_of_construction >='] = $w['year_of_construction1'];
        if (isset($w['year_of_construction2']))
            $where['year_of_construction <='] = $w['year_of_construction2'];
        if (isset($w['status']))
            $where['status'] = $w['status'];
        if (isset($w['customer_source']))
            $where['customer_source'] = $w['customer_source'];

        if (isset($w['sctime'])) {
            $ts = explode("/", $w['sctime']);
            $where['ctime >='] = mktime(0, 0, 0, $ts[1], $ts[2], $ts[0]);
        }
        if (isset($w['ectime'])) {
            $te = explode("/", $w['ectime']);
            $where['ctime <='] = mktime(23, 59, 59, $te[1], $te[2], $te[0]);
        }
        if (isset($w['broker']))
            $this->set_or_like('broker', $w['broker']);
        $user_purview = check_broker_is_visible();
        if (!$user_purview) {
            $this->set_or_like('front_man,broker', $this->login_lib->users_name());
        }
        $where['dtime'] = 0;
        return $this->fetch(NULL, $where, NULL, "count");
    }

    function update_demand($data, $id) {

        $where[] = array('demand_id' => $id);
        return $this->update($data, $where);
    }

    function insert_demand_history($data, $id) {

        $query = $this->db->get_where('h_demand_history', array('demand_id' => $id));

        //echo 'kakai';
        //$data['shop_point_id'] = $this->login_lib->shop_point_id();
        $data['ctime'] = time();
        $data['demand_id'] = $id;
        $data['version'] = $query->num_rows()+1;

        $this->db->insert('h_demand_history', $data);
    }

    function do_update_demand_by_victor($data, $id) {

        $this->db->where($this->_key_name, $id);
        $this->db->update($this->_table_name, $data);
        return 1;
    }

    function add_demand($data) {

        $i = "";
        if ($data['type_rent'] == "1")
            $i = $this->add_demand_rent($data);
        if ($data['type_sell'] == "1")
            $i = $this->add_demand_sell($data);
        return $i;
    }

    function add_demand_by_victor($data) {

        if ($data['type_rent'] == "1")
            $i = $this->add_demand_rent($data);

        if ($data['type_sell'] == "1")
            $i = $this->add_demand_sell($data);

        if ($data['type_consignment'] == "1")
            $i = $this->add_demand_type_consignment($data);
        //add_demand_type_consignment
    }

    function add_demand_rent($data) {
        foreach ($data as $key => $val) {
            if ($key != "reported_rate1" &&
                    $key != "reported_rate2" &&
                    $key != "type_sell" &&
                    $key != "type_consignment")
                $dr[$key] = $val;
        }
        $dr['shop_point_id'] = $this->login_lib->shop_point_id();
        $dr['ctime'] = time();
        //print_r($dr);die();
        return $this->insert($dr);
    }

    function add_demand_sell($data) {
        foreach ($data as $key => $val) {
            if ($key != "leasing_business_purposes" &&
                    $key != "type_rent" &&
                    $key != "type_consignment")
                $ds[$key] = $val;
        }


        $ds['shop_point_id'] = $this->login_lib->shop_point_id();

        $ds['ctime'] = time();
        return $this->insert($ds);
    }

    function add_demand_type_consignment($data) {

        foreach ($data as $key => $val) {
            if ($key != "type_rent" &&
                    $key != "type_sell")
                $ds[$key] = $val;
        }


        //$ds['shop_point_id']=
        //$this->login_lib->shop_point_id();

        $ds['ctime'] = time();
        return $this->insert($ds);
    }

    function add_demand_consignment($data) {

        $data['shop_point_id'] = $this->login_lib->shop_point_id();
        $data['ctime'] = time();
        return $this->insert($data);
    }

    function object_match_demand($object_where) {
        $this->db->where($object_where);
        $query = $this->db->get("demand");
        return $query;
    }

    function del_demand_id($id) {

        $this->db->where($this->_key_name, $id);
        $this->db->delete($this->_table_name);
    }

    function del_demand($data) {
        $where[] = array('demand_id' => $data['id']);
        $value = array('dtime' => time());
        return $this->update($value, $where);
    }

    function countRec($table_name, $query) {

        $sql = " SELECT count(*) as `count` FROM `h_demand` " . $query;

        $query = $this->db->query($sql);

        $total;

        foreach ($query->result() as $row) {
            $total = $row->count;
        }

        return $total;
    }

    function demand_for_crid($page
    , $rp
    , $sortname
    , $sortorder
    , $query) {


        $roder_by = " ORDER BY  `" . $sortname . "` " . $sortorder . " ";

        $start = (($page - 1) * $rp);
        $limit = "LIMIT $start, $rp";

        $sql_round = " SELECT * FROM  " .
                "  `h_demand` " .
                $query . " " .
                $roder_by . " " .
                $limit;


        $total_rows = $this->countRec("h_demand", $query);

        $query = $this->db->query($sql_round);
        return $query;
    }

}
